﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EcoSystem
{
    public static class EnumExtensions
    {
        public static string Description(this Enum value)
        {
            var type = value.GetType();
            if (type.IsEnum)
            {

                FieldInfo fieldInfo = type.GetField(value.ToString());
                DescriptionAttribute attr = Attribute.GetCustomAttribute(
                    fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (attr != null && !string.IsNullOrEmpty(attr.Description))
                {
                    return attr.Description;
                }
                else
                {
                    return fieldInfo.Name.ToString();
                }
            }
            else
            {
                throw new Exception("枚举信息错误");
            }

        }
    }
}
